//
//  CCObject.m
//  GGTV
//
//  Created by apple on 15/10/28.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "CCObject.h"

@implementation CCObject

+ (instancetype)modelWithDictionary: (NSDictionary *)dictionary
{
    return [[self alloc] initModelWithDictionary:dictionary];
}

-(instancetype)initModelWithDictionary:(NSDictionary *)dictionary
{
    if (self = [super init]) {
         [self assginToPropertyWithDictionary:dictionary];
    }
    return self;
}

/************************************************************************
 *把字典赋值给当前实体类的属性
 *参数：字典
 *适用情况：当网络请求的数据的key与实体类的属性相同时可以通过此方法吧字典的Value
 *        赋值给实体类的属性
 ************************************************************************/

-(void) assginToPropertyWithDictionary: (NSDictionary *)dictionary
{
    
    if (dictionary == nil) {
        return;
    }
    ///1.获取字典的key
    NSArray *dicKey = [dictionary allKeys];
    ///2.循环遍历字典key, 并且动态生成实体类的setter方法，把字典的Value通过setter方法
    ///赋值给实体类的属性
    for (int i = 0; i < dicKey.count; i ++) {
        
        ///2.1 通过getSetterSelWithAttibuteName 方法来获取实体类的set方法
        NSString *strName = [self creatSetterWithPropertyName:dicKey[i]];

        ///2.2 获取字典中key对应的value
        
        id value;
        
        if ([dictionary[dicKey[i]] isKindOfClass:[NSArray class]] || [dictionary[dicKey[i]] isKindOfClass:[NSDictionary class]] || [dictionary[dicKey[i]] isKindOfClass:[NSNumber class]] ) {
            value = dictionary[dicKey[i]];
        }else{
            value = [NSString stringWithFormat:@"%@", dictionary[dicKey[i]]];
        }
        [self setValue:value forKey:strName];
    }
}

-(NSString *)creatSetterWithPropertyName:(NSString *)propertyName
{
    unsigned int count = 0;
    NSString *tempPropertyName = @"";
    Ivar *ivar = class_copyIvarList([self class], &count);
    for (int i = 0; i<count; i++) {
        Ivar iva = ivar[i];
        const char *name = ivar_getName(iva);
        tempPropertyName = [NSString stringWithUTF8String:name];
        
        //1.首字母大写
        NSString *tempName = tempPropertyName.capitalizedString;
        tempName = [tempPropertyName substringFromIndex:1];
        
        
        if ([tempName compare:propertyName options:NSCaseInsensitiveSearch] == NSOrderedSame) {
            return tempPropertyName;
            //2.拼接上set关键字
        }
    }
    return nil;
}


//解档
- (id)initWithCoder:(NSCoder *)decoder
{
    if (self = [super init]) {
        unsigned int count = 0;
        //获取类中所有成员变量名
        Ivar *ivar = class_copyIvarList([self class], &count);
        for (int i = 0; i<count; i++) {
            Ivar iva = ivar[i];
            const char *name = ivar_getName(iva);
            NSString *strName = [NSString stringWithUTF8String:name];
            //进行解档取值
            id value = [decoder decodeObjectForKey:strName];
            //利用KVC对属性赋值
            [self setValue:value forKey:strName];
        }
        free(ivar);
    }
    return self;
}

//归档
- (void)encodeWithCoder:(NSCoder *)encoder
{
    unsigned int count;
    Ivar *ivar = class_copyIvarList([self class], &count);
    for (int i=0; i<count; i++) {
        Ivar iv = ivar[i];
        const char *name = ivar_getName(iv);
        NSString *strName = [NSString stringWithUTF8String:name];
        //利用KVC取值
        id value = [self valueForKey:strName];
        [encoder encodeObject:value forKey:strName];
    }
    free(ivar);
}

@end
